package com.example.admin.smstest.utils;

import java.util.Arrays;
import java.util.Locale;

/**
 * 对byte数组的各种运算
 */

public class ByteUtil {
    /**
     * byte截取数组
     *
     * @param bytes
     *            （原始数据）
     * @param begin
     *            （开始截取位置）
     * @param count
     *            （截取长度）
     * @return
     */
    public static byte[] cutBytes(byte[] bytes, int begin, int count) {
        byte[] bt = new byte[count];
        System.arraycopy(bytes, begin, bt, 0, count);
        return bt;
    }

    /**
     * int --> Hex --> byte
     * @param i
     * @return
     */
    public static byte[] intToHexBytes(int i){
        String hexString = null;
        if(Integer.toHexString(i).length() % 2 != 0){
            hexString = "0"+Integer.toHexString(i);
        } else {
            hexString = Integer.toHexString(i);
        }
        return hexStringToBytes(hexString);
    }

    /**
     * hexString --> Bytes
     * @param hexString
     * @return
     */
    public static byte[] hexStringToBytes(String hexString) {
		 /*对输入值进行规范化整理*/
        hexString = hexString.trim().replace(" ", "").toUpperCase(Locale.US);
        //处理值初始化
        int m=0,n=0;
        int iLen=hexString.length()/2; //计算长度
        byte[] ret = new byte[iLen]; //分配存储空间

        for (int i = 0; i < iLen; i++){
            m=i*2+1;
            n=m+1;
            ret[i] = (byte)(Integer.decode("0x"+ hexString.substring(i*2, m) + hexString.substring(m,n)) & 0xFF);
        }
        return ret;
    }

    /**
     * 多个数组合并
     *
     * @param first
     * @param rest
     * @return
     */
    public static byte[] concatAll(byte[] first, byte[]... rest) {
        int totalLength = first.length;
        for (byte[] array : rest) {
            totalLength += array.length;
        }
        byte[] result = Arrays.copyOf(first, totalLength);
        int offset = first.length;
        for (byte[] array : rest) {
            System.arraycopy(array, 0, result, offset, array.length);
            offset += array.length;
        }
        return result;
    }

    /**
     * 通过输入String类型的content
     * 输出content的长度（Byte)+content(Byte)
     * @param content
     * @return
     */
    public static byte[] getByte(String content){
        try {
            byte[] conByte = content.getBytes("UNICODE");
            byte[] contentByte= ByteUtil.cutBytes(conByte,2,conByte.length-2);//通过编码最前方有2Byte的占时无用字节，去掉
            byte[] contentByteLength = ByteUtil.intToHexBytes(contentByte.length);
            return concatAll(contentByteLength,contentByte);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取包数
     * @param i
     * @return
     */
    public static byte[] getPackets(int i){
        String hex = getTurnString(DateUtil.getString(Integer.toHexString(i),4));
        return hexStringToBytes(hex);
    }

    /**
     * 使偶数String数据进行反转
     * @param s
     * @return
     */
    public static String getTurnString(String s){
        String test = "";
        System.out.println(s.length());
        for(int i = s.length()/2; i > 0;i-- ){
            test += s.substring((i-1)*2, 2*i);
        }
        return test;
    }
}
